package cn.com.fuhuitong.utils

import android.graphics.Color
import android.text.Spannable
import android.text.SpannableString
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StrikethroughSpan
import android.widget.TextView
import androidx.core.text.set
import java.text.DecimalFormat

object SpanUtils {
    //    /**
//     * 传入金额，拼接人民币符号，并设置符号字体为原字体的60%
//     */
//    fun setMoneySymbol(price: String, showDivider: Boolean = false) =
//        buildSpannedString {
//            scale(0.7f) { append("￥") }.append(price)
//            if (showDivider) append("/")
//        }
//
//    /**
//     * 传入金额，生成"xx￥xxx"格式的金额
//     */
//    fun setPrefixMoneyStr(prefix: String = "赚", price: String): SpannedString =
//        buildSpannedString { append(prefix).append("￥").scale(1.2f) { bold { append(price) } } }
//
//    /**
//     * 传入金额，生成"xx￥xxx"格式的金额
//     */
//    fun setPrefixMoneyStr2(prefix: String = "赚", price: String): SpannedString =
//        buildSpannedString { append(prefix).scale(1.2f) { bold { append(price) } } }

    fun setMoneyStyle(
        price1: Double, price2: Double = 0.00, isDivider: Boolean = false
    ): Spannable {
        if (price1 == 0.0) {
            val string: Spannable = SpannableString("￥0.00")
            string.setSpan(
                AbsoluteSizeSpan(37), 0, 1,
                Spannable.SPAN_INCLUSIVE_INCLUSIVE
            )
            return string
        }

        val df = DecimalFormat("############0.00")

        val priceString1 = "￥${df.format(price1)} "
        val priceString2 = "￥${df.format(price2)}"

        val priceMerge = "${priceString1}${if (price2 != 0.00) priceString2 else ""}"

        val string: Spannable = SpannableString(priceMerge)
        string.setSpan(
            AbsoluteSizeSpan(37), 0, 1,
            Spannable.SPAN_INCLUSIVE_INCLUSIVE
        )
        if (price2 == 0.00) {
            return string
        } else {
            val priceMergeLength: Int = priceMerge.length
            val priceString2Length: Int = priceString2.length
            val start: Int = priceMergeLength - priceString2Length
            string.setSpan(
                StrikethroughSpan(), start, priceMergeLength, Spannable.SPAN_INCLUSIVE_INCLUSIVE
            )
            string.setSpan(
                ForegroundColorSpan(Color.parseColor("#FF999999")),
                start, priceMergeLength,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            string.setSpan(
                AbsoluteSizeSpan(37), start, priceMergeLength, Spannable.SPAN_INCLUSIVE_INCLUSIVE
            )
            return string
        }
    }

    fun decimalFormat(money: Double): String {
        val df = DecimalFormat("############0.00")
        return "￥${df.format(money)}"
    }

    /**
     * 设置金额符号为原视图的一半
     * */
    fun setMoneySymbol(textView: TextView, money1: Double, isSymbol: Boolean = true): Spannable {
        val afterMoney: Spannable = SpannableString(decimalFormat(money1))
        if (!isSymbol) {
            return afterMoney
        }
        afterMoney.setSpan(
            AbsoluteSizeSpan((textView.textSize / 2).toInt()), 0, 1,
            Spannable.SPAN_INCLUSIVE_INCLUSIVE
        )
        return afterMoney
    }

//    /**
//     * 设置划线价格
//     * */
//    fun setMoneyScribing(
//        textView: TextView, money1: Double, money2: Double = 0.00, isSymbol: Boolean = true
//    ) {
//        val symbolMoney: Spannable = this.setMoneySymbol(textView, money1, isSymbol)
//
//        val
//        val afterMoney: Spannable = SpannableString(decimalFormat(money2))
//
//        afterMoney.setSpan(
//            StrikethroughSpan(), 0, priceMergeLength, Spannable.SPAN_INCLUSIVE_INCLUSIVE
//        )
//
//        symbolMoney.set()
//    }

}